package com.lishe.lishe.lishe.module.mall.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.lishe.lishe.R;
import com.lishe.lishe.lishe.MyRecyclerView.RecyclerviewBaseAdapter;
import com.lishe.lishe.lishe.MyRecyclerView.SpaceItemDecoration2;
import com.lishe.lishe.lishe.base.BaseActivity;
import com.lishe.lishe.lishe.bean.SpaceItemDecoration;
import com.lishe.lishe.lishe.module.mall.adapter.Classification1Adapter;
import com.lishe.lishe.lishe.module.mall.adapter.Classification2Adapter;
import com.lishe.lishe.lishe.module.mall.bean.category.CategoryFirst;
import com.lishe.lishe.lishe.module.mall.http.HttpMall;
import com.lishe.lishe.lishe.module.mall.presenter.ClassificationPresenter;
import com.lishe.lishe.lishe.utils.BitmapUtils;
import com.lishe.lishe.lishe.utils.Utils;
import com.lishe.lishe.retrofit.DataResponseListener;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.smarttop.library.utils.LogUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cz.msebera.android.httpclient.Header;

/**
 * Created by yang on 2017/11/6.
 */

public class ClassificationActivity extends BaseActivity implements View.OnClickListener {

    public static void jump(Activity activity) {
        Intent intent = new Intent(activity, ClassificationActivity.class);
        activity.startActivity(intent);
    }

    private Button mBack;
    private TextView mTitle, mEmptyText;
    private RecyclerView mRecycler1, mRecycler2;
    private LinearLayout mLoading, mLoading2;
    private RelativeLayout mEmpty, mError, mEmpty1, mError1;

    private List<CategoryFirst> mList1;
    private List<CategoryFirst> mList2;
    private Classification1Adapter mAdapter1;
    private Classification2Adapter mAdapter2;
    private int selectPosition = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_classification);
        initView();
        setListener();
        setAdapter();
        getCategoryList1();
    }

    private void initView() {
        mBack = findViewById(R.id.title_left_button);
        mTitle = findViewById(R.id.title_text_title);
        mRecycler1 = findViewById(R.id.level1_classification);
        mRecycler2 = findViewById(R.id.level2_classification);
        mLoading = findViewById(R.id.loading);
        mLoading2 = findViewById(R.id.loading2);
        mEmpty = findViewById(R.id.empty);
        mEmpty1 = findViewById(R.id.empty1);
        mError = findViewById(R.id.error);
        mError1 = findViewById(R.id.error1);
        mEmptyText = findViewById(R.id.empty_text);

        mTitle.setText("商品分类");
        mRecycler1.setLayoutManager(new LinearLayoutManager(ClassificationActivity.this, LinearLayoutManager.VERTICAL, false));
        mRecycler2.setLayoutManager(new GridLayoutManager(ClassificationActivity.this, 3));

        mRecycler1.addItemDecoration(new SpaceItemDecoration(0, 0, 0, Utils.dip2px(ClassificationActivity.this, 1)));
        mRecycler2.addItemDecoration(new SpaceItemDecoration2(this, Utils.dip2px(ClassificationActivity.this, 15),
                Utils.dip2px(ClassificationActivity.this, 15),
                Utils.dip2px(ClassificationActivity.this, 15),
                0,
                Utils.dip2px(ClassificationActivity.this, 34),
                new SpaceItemDecoration2.DecorationCallback() {

            @Override
            public String getGroupFirstLine(int position) {
                return mList2.get(position).getCate_name();
            }
        }));

        mEmptyText.setText("还没有该分类哦~");
    }

    private void setListener() {
        mBack.setOnClickListener(this);
        mError1.setOnClickListener(this);
        mError.setOnClickListener(this);

    }

    private void setAdapter() {
        mList1 = new ArrayList<>();
        mList2 = new ArrayList<>();
        mAdapter1 = new Classification1Adapter(ClassificationActivity.this, mList1);
        mAdapter2 = new Classification2Adapter(ClassificationActivity.this, mList2, BitmapUtils.getDisplayWidth(this));
        mRecycler1.setAdapter(mAdapter1);
        mRecycler2.setAdapter(mAdapter2);

        mAdapter1.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                if (selectPosition != position) {
                    mAdapter1.setSelectPosition(position);
                    getCategoryList2(mList1.get(position).getCate_id());
                    selectPosition = position;
                }

            }
        });

        mAdapter2.setOnItemClickListener(new RecyclerviewBaseAdapter.ItemListener.ItemClickListener() {
            @Override
            public void OnClickListener(RecyclerView.Adapter adapter, View view, int position) {
                Log.e("TAG", mList2.get(position).toString());
                if (!mList2.get(position).isSecond()) {
                    ClassificationMallActivity.jump(ClassificationActivity.this, mList2.get(position).getCate_id(), mList2.get(position).getOriginal_cate_id(), false);
                }
            }
        });


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_button:
                finish();
                break;
            case R.id.error1:
                getCategoryList1();
                break;
            case R.id.error:
                getCategoryList2(mList1.get(selectPosition).getCate_id());
                break;
        }
    }

    /**
     * 获取首页一级分类列表
     */
    public void getCategoryList1() {
        if (mEmpty1.getVisibility() == View.VISIBLE) {
            mEmpty1.setVisibility(View.GONE);
        }
        if (mError1.getVisibility() == View.VISIBLE) {
            mError1.setVisibility(View.GONE);
        }

        if (mLoading.getVisibility() == View.GONE) {
            mLoading.setVisibility(View.VISIBLE);
        }
        try {
            ClassificationPresenter.getInstance().categoryFirstList(new DataResponseListener<List<CategoryFirst>>() {
                @Override
                public void onSuccess(List<CategoryFirst> dataResponse) {
                    mLoading.setVisibility(View.GONE);


                    mList1.clear();
                    mList1.addAll(dataResponse);
                    if (mList1.size() > 0) {
                        mEmpty1.setVisibility(View.GONE);
                        mRecycler2.setVisibility(View.VISIBLE);

                        mAdapter1.notifyDataSetChanged();
                        getCategoryList2(mList1.get(0).getCate_id());
                    }
                }

                @Override
                public void onFail(String failMessage) {
                    mLoading.setVisibility(View.GONE);

                    mEmpty1.setVisibility(View.VISIBLE);
                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    mLoading.setVisibility(View.GONE);
                    mError1.setVisibility(View.VISIBLE);
                    mLoading.setVisibility(View.GONE);

                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            mLoading.setVisibility(View.GONE);
            mError1.setVisibility(View.VISIBLE);

            Log.e("TAG", e.toString());
            toast(getString(R.string.data_parse_fail));
        }

    }

    /**
     * 获取首页二级分类列表
     */
    public void getCategoryList2(String cate_id) {
        if (mEmpty.getVisibility() == View.VISIBLE) {
            mEmpty.setVisibility(View.GONE);
        }
        if (mError.getVisibility() == View.VISIBLE) {
            mError.setVisibility(View.GONE);
        }

        if (mLoading2.getVisibility() == View.GONE) {
            mLoading2.setVisibility(View.VISIBLE);
        }
        mList2.clear();
        mAdapter2.notifyDataSetChanged();
        try {
            ClassificationPresenter.getInstance().categorySecondList(cate_id, new DataResponseListener<List<CategoryFirst>>() {
                @Override
                public void onSuccess(List<CategoryFirst> dataResponse) {
                    mLoading.setVisibility(View.GONE);
                    mLoading2.setVisibility(View.GONE);

                    mList2.clear();
                    if (dataResponse != null) {
                        for (CategoryFirst category : dataResponse) {
                            category.setSecond(true);
                            mList2.add(category);

                            if (category.getSon() != null)
                                mList2.addAll(category.getSon());
                        }
                    }

                    if (mList2.size() > 0) {
                        mEmpty.setVisibility(View.GONE);
                        mError.setVisibility(View.GONE);
                        mAdapter2.notifyDataSetChanged();
                    } else {
                        mEmpty.setVisibility(View.VISIBLE);
                    }
                }

                @Override
                public void onFail(String failMessage) {
                    mLoading.setVisibility(View.GONE);
                    mLoading2.setVisibility(View.GONE);
                    mEmpty.setVisibility(View.VISIBLE);

                    toast(failMessage);
                }

                @Override
                public void onError(Throwable e) {
                    mLoading2.setVisibility(View.GONE);
                    mError.setVisibility(View.VISIBLE);
                    mLoading.setVisibility(View.GONE);

                    toast(R.string.network_or_server_exception);
                }
            });
        } catch (Exception e) {
            Log.e("TAG", e.toString());
            toast(getString(R.string.data_parse_fail));
        }
    }
}
